//
//  WBEmotionKeyboard.m
//  新浪微博项目
//
//  Created by neng on 15/12/21.
//  Copyright (c) 2015年 neng. All rights reserved.
//

#import "WBEmotionKeyboard.h"
#import "WBEmotionListView.h"
#import "WBEmotionTabBar.h"

#import "WBEmotion.h"
#import "WBEmotionTool.h"

@interface WBEmotionKeyboard () <WBEmotionTabBarDelegate>

@property (nonatomic,weak) WBEmotionTabBar  *emotionTabBar;

//容器
@property (nonatomic,weak) UIView *contentView;

//四个表情列表
@property (nonatomic,strong) WBEmotionListView *recentEmotionList;
@property (nonatomic,strong) WBEmotionListView *defaultEmotionList;
@property (nonatomic,strong) WBEmotionListView *emojiEmotionList;
@property (nonatomic,strong) WBEmotionListView *lxhEmotionList;

@end

@implementation WBEmotionKeyboard

#pragma mark - 懒加载
- (WBEmotionListView *)recentEmotionList
{
    if (!_recentEmotionList) {
        _recentEmotionList = [[WBEmotionListView alloc] init];
        _recentEmotionList.emotions = [WBEmotionTool recentEmotions];
    }
    return _recentEmotionList;
}
- (WBEmotionListView *)defaultEmotionList
{
    if (!_defaultEmotionList) {
        _defaultEmotionList = [[WBEmotionListView alloc] init];
        _defaultEmotionList.emotions = [WBEmotionTool defaultEmotions];
    }
    return _defaultEmotionList;
}
- (WBEmotionListView *)emojiEmotionList
{
    if (!_emojiEmotionList) {
        _emojiEmotionList = [[WBEmotionListView alloc] init];
        _emojiEmotionList.emotions = [WBEmotionTool emojiEmotions];
    }
    return _emojiEmotionList;
}
- (WBEmotionListView *)lxhEmotionList
{
    if (!_lxhEmotionList) {
        _lxhEmotionList = [[WBEmotionListView alloc] init];
        _lxhEmotionList.emotions = [WBEmotionTool lxhEmotions];
    }
    return _lxhEmotionList;
}

#pragma 初始化
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        //自己的frame
        self.width = 200;
        self.height = 216;
        self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"emoticon_keyboard_background"]];
        
        //1.表情列表容器
        UIView *contentView = [[UIView alloc] init];
        [self addSubview:contentView];
        self.contentView = contentView;
        
        
        //2.表情tabBar
        WBEmotionTabBar *emotionTabBar = [[WBEmotionTabBar alloc] init];
        emotionTabBar.delegate = self;
        [self addSubview:emotionTabBar];
        self.emotionTabBar = emotionTabBar;
        
        //监听插入表情的通知， 因为在这里能拿到最近表情的view来给它赋值
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(emotionClick:) name:WBEmotionKeyboardDidClickedEmotionKey object:nil];
        
    }
    return self;
}

//点击了表情
- (void)emotionClick:(NSNotification *)noti
{
    //取出表情
    WBEmotion *emotion = noti.userInfo[WBEmotionKey];
    [WBEmotionTool addEmotion:emotion];
    
    self.recentEmotionList.emotions = [WBEmotionTool recentEmotions];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    
    //1.表情tabBar
    self.emotionTabBar.width = self.width;
    self.emotionTabBar.height = 37;
    self.emotionTabBar.x = 0;
    self.emotionTabBar.y = self.height - self.emotionTabBar.height;
    
    //2.表情列表容器
    self.contentView.width = self.emotionTabBar.width;
    self.contentView.height = self.height - self.emotionTabBar.height;
    self.contentView.x = 0;
    self.contentView.y = 0;
    
    //3.表情列表的frame
    //如果懒加载就initWithFrameWith:self.contentView.frame。有可能表情列表的frame为0
    self.recentEmotionList.frame = self.contentView.frame;
    self.defaultEmotionList.frame = self.contentView.frame;
    self.emojiEmotionList.frame = self.contentView.frame;
    self.lxhEmotionList.frame = self.contentView.frame;
}

#pragma WBEmotionTabBarDelegate
- (void)emotionTabBar:(WBEmotionTabBar *)tabBar didClickedBtnWithType:(WBEmotionTabBarButtonType)type
{
    switch (type) {
        case WBEmotionTabBarButtonTypeRecent:
            //移除上个表情列表
            [self.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
            //添加对应的表情列表
            [self.contentView addSubview:self.recentEmotionList];
            break;
        case WBEmotionTabBarButtonTypeDefault:
            //移除上个表情列表
            [self.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
            //添加对应的表情列表
            [self.contentView addSubview:self.defaultEmotionList];
            break;
        case WBEmotionTabBarButtonTypeEmoji:
            //移除上个表情列表
            [self.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
            //添加对应的表情列表
            [self.contentView addSubview:self.emojiEmotionList];
            break;
        case WBEmotionTabBarButtonTypeLxh:
            //移除上个表情列表
            [self.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
            //添加对应的表情列表
            [self.contentView addSubview:self.lxhEmotionList];
            break;
        default:
            break;
    }
}

@end
